package com.summer.main;

import java.math.BigDecimal;

public class UnitTools {
	private static final String[] testValueList = {
		"2",
		"12",	
		"132",	
		"1234",	
		"12345",	
		"123456",	
		"1234567",	
		"14567894",	
		"112345789",	
		"1156487999",	
		"11789486158",	
		"188864513348",	
		"1485949789999",	
		"17897715615677",	
		"215791268575416",	
		"1456718154894561",	
		"11249687971894897",	
		"112348941532156789",	
		"3156891234468845315",	
		"11265448581878187129",	
		"158974818638456891545",	
		"1456891115489456156489",	
		"11512315489156489195465",	
		"198456815456898189156489",	
		"1156489918891898484489489",	
		"11596898489189187895555578",	
		"115968984891891878955555782",	
		"1159689848918918789555557822",	
		"11596898489189187895555578221",	
		"115968984891891878955555782212",	
		"2115968984891891878955555782212",	
		"32115968984891891878955555782212",	
		"232115968984891891878955555782212",	
		"3232115968984891891878955555782212",	
		"43232115968984891891878955555782212",	
		"543232115968984891891878955555782212",	
		"6543232115968984891891878955555782212",	
		"76543232115968984891891878955555782212",	
		"876543232115968984891891878955555782212",	
		"9876543232115968984891891878955555782212",	
		"19876543232115968984891891878955555782212",	
	};
	public static void main(String[] args) {
		for(String value : testValueList){
			System.out.println(coinsUnitConversion(value));
		}
	}
	
	public static String unitConversion(String value,int startSize,int weightSize,String[] units){
		//检查起始转换位置，数值达到几位数开始转换，小于这个位数则不转换
	    int length	= value.length() - 1;
	    if(length < startSize)
	        return value;
	    
	    //计算数值长度达到第几个单位权重，如果大于单位范围，则取最大单位权重进行转换
	    int size	= Math.min((length/weightSize),units.length-1);
	    
	    //获得单位基准数值， 单位换算：单位值 =  数值/单位基准数，保留两位小数，如   123456，转换成K单位，则 123456/1000得123.47K,四舍五入
	    BigDecimal valueBig		= new BigDecimal(value);
		BigDecimal weightValue	= new BigDecimal(10).pow(weightSize);
	    BigDecimal baseUnit		= weightValue.pow(size);
	    
	    double newValue = valueBig.divide(baseUnit,2, BigDecimal.ROUND_HALF_UP).doubleValue();
	    return newValue + units[size];
	}
	
	public static String sunUnitConversion(String sunValue){
	    int weightSize  = 3; //单位进制规则，3位一进
	    int startSize	= 5; //起始限制，大于5位数则转换
//		String[] units = {"k","k","m","b","t","a"};
		String[] units = {"k","k","m","b","t","a","aa","ab","ac","ad","ae","af","ag","ah","ay","aj","ak","al","am","ao","ap","aq","ax","ay","az"};    
	    return unitConversion(sunValue,startSize,weightSize,units);
	}
	
	public static String coinsUnitConversion(String coinsValue){
		int weightSize	= 4; 
		int startSize	= 5;
		String[] units	= {"万","万","亿"};
		return unitConversion(coinsValue,startSize,weightSize,units);
	}

}